package j.util.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import j.util.New;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * User: jason
 * Date: 14-9-7 下午3:22
 */
public class JsonUtils {
    private static SerializeConfig config = new SerializeConfig();
    static{
        DateSerializer ds = new DateSerializer();
        config.put(Date.class,ds);
        config.put(java.sql.Date.class,ds);
        config.put(Timestamp.class,ds);
        config.put(Time.class,ds);
    }
    public static String toJson(Object object){
        return JSON.toJSONString(object, config,
                SerializerFeature.BrowserCompatible,SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteMapNullValue);
    }

    public static Map<String,Object> parseMap(String json){
        return JSON.parseObject(json);
    }
    public static List<Map<String,Object>> parseListMap(String json){
        JSONArray j = JSON.parseArray(json) ;
        List<Map<String,Object>> listMap = New.list();
        for (Object o : j) {
            listMap.add((JSONObject)o);
        }
        return listMap;
    }

    public static <T> List<T> parseArray(String json,Class<T> clazz){
        return JSON.parseArray(json, clazz);
    }

    public static <T> T parseBean(String json,Class<T> clazz){
        return JSON.parseObject(json,clazz);
    }

}
